/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cannes.dao.implement;

import cannes.bean.planning.Personne;
import cannes.bean.vip.Vip;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import cannes.dao.DAOFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author p1000666
 */
public class VipDAO extends DAO<Vip> {

    public VipDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Vip obj) {
        String req = null;
        if(obj.getCompagnonId() == 0){
            req = "INSERT INTO vip  VALUES ('"+obj.getId()+"',"
                + " NULL ,'"+obj.getType()+"','"+obj.getAttrait()+"')";
        }else{
            req = "INSERT INTO vip  VALUES ('"+obj.getId()+"',"
                + "'"+ obj.getCompagnonId() +"','"+obj.getType()+"','"+obj.getAttrait()+"')";
        }
       
        System.out.println(req);
        try {
            Personne p = new Personne(obj.getId(),obj.getNom(),obj.getPrenom(),obj.getPhoto(),obj.getNationalite());
            DAO<Personne> per ;
            per = DAOFactory.getPersonneDAO();
            per.create(p);
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(req);
            return result.rowInserted();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean update(Vip obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "UPDATE vip SET compagnonid =" + obj.getCompagnonId() +",typevip = '" + obj.getType() + "',attrait = " + obj.getAttrait() + "WHERE vipid = " + obj.getId());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return false;
    }

    @Override
    public boolean delete(Vip obj) {
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "DELETE FROM vip WHERE vipid = " + obj.getId());
            result.rowDeleted();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public int next() {
        int i = 0;
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT max(vipid) from vip");
            if (result.first()) i = result.getInt(1)+1;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }


    @Override
    public Vip find(int id) {
        Vip vip = new Vip();

        try {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM vip JOIN personne ON personne.personneid = vip.vipid"
                    +" WHERE vipid = " + id);
            if (res.first()) {
                vip = new Vip(res.getInt("vipid"), res.getInt("compagnonid"), res.getString("typevip"),
                        res.getInt("attrait"),res.getString("nom"), res.getString("prenom"),
                        res.getString("photo"), res.getString("nationalite"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return vip;

    }

    @Override
    public ArrayList<Vip> findAll() {
        ArrayList<Vip> list = new ArrayList<Vip>();
        Vip vip;

        try
        {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM vip JOIN personne ON personne.personneid = vip.vipid");
            while (res.next())
            {
                vip = new Vip(res.getInt("vipid"), res.getInt("compagnonid"), res.getString("typevip"),
                        res.getInt("attrait"),res.getString("nom"), res.getString("prenom"),
                        res.getString("photo"), res.getString("nationalite"));
                list.add(vip);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
